package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Page<Dish> findByPage(String name, Integer pageNum, Integer pageSize) {
        //构建条件，根据菜品名称从菜品表中查询
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);

        //构建分页条件
        Page<Dish> page = new Page<>(pageNum, pageSize);

        //根据分页查询菜品信息
        page = dishMapper.selectPage(page,wrapper);

        //遍历菜品集合
        List<Dish> dishList = page.getRecords();
        if (CollectionUtils.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //根据category_id从分类列表中查询分类名称
                Category category = categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());

                //根据菜品的id从口味表中查询当前菜品的口味信息
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavorList);//封装当前菜品的口味列表
            }
        }
        return page;
    }

    @Override
    public void save(Dish dish) {
        //将dish基本信息保存到菜品表
        dishMapper.insert(dish);

        //将dish中的口味列表，遍历保存到口味表
        List<DishFlavor> dishFlavorList = dish.getFlavors();
        if (CollectionUtils.isNotEmpty(dishFlavorList)) {
            for (DishFlavor dishFlavor : dishFlavorList) {
                dishFlavor.setDishId(dish.getId());
                //保存一个口味
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public Dish findById(Long id) {
        //根据id查询菜品信息
        Dish dish = dishMapper.selectById(id);

        //根据菜品id查询口味信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper);
        dish.setFlavors(dishFlavorList);//封装当前菜品的口味列表

        return dish;
    }

    @Override
    public void update(Dish dish) {
        //根据主键修改菜品表中的信息
        dishMapper.updateById(dish);

        //根据菜品id删除口味表中相关口味
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(wrapper);

        //向口味表中添加新的口味列表
        List<DishFlavor> dishFlavorList = dish.getFlavors();
        if (CollectionUtils.isNotEmpty(dishFlavorList)) {
            for (DishFlavor dishFlavor : dishFlavorList) {
                dishFlavor.setDishId(dish.getId());
                //保存一个口味
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public void deleteByIds(Long[] ids) {
        //判断要删除的商品是否全部停售
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId, ids);
        wrapper.eq(Dish::getStatus, 1);
        Integer count = dishMapper.selectCount(wrapper);
        if (count > 0) {
            throw new CustomException("要删除的菜品还在在售状态，不能删除");
        }

        for (Long id : ids) {
            //根据菜品删除口味
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId, id);
            dishFlavorMapper.delete(wrapper1);

            //删除菜品
            dishMapper.deleteById(id);
        }
    }

    @Override
    public void updateByIds(Integer status, Long[] ids) {

        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishMapper.updateById(dish);
        }
    }

    @Override
    public List<Dish> findList(Long categoryId, Integer status,String name) {
        //设置查询条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, categoryId);
        wrapper.eq(Dish::getStatus, status);
        List<Dish> dishList = dishMapper.selectList(wrapper);

        if (CollectionUtils.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);

                dish.setFlavors(dishFlavorList);
            }
        }
        if (name != null) {
            LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(Dish::getStatus, status);
            wrapper1.like(Dish::getName, name);
            dishList = dishMapper.selectList(wrapper1);
        }
        return dishList;
    }


}
